<template>
  <el-dialog
    v-dialogDrag
    :visible.sync="show"
    :width="width"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :show-close="false"
    @close="handleClose"
    :append-to-body="atob"
    :fullscreen="fullscreen"
    :id="uid"
  >
    <template slot="title">
      <div class="flex-between">
        <span>{{ title }}</span>
        <div class="flex">
          <i
            class="el-icon-full-screen pointer"
            style="margin-right: 30px"
            @click="handleFullscreen"
            v-if="showFullscreen"
          ></i>
          <i class="el-icon-close pointer" @click="show = !show"></i>
        </div>
      </div>
    </template>
    <slot></slot>
    <slot name="button" slot="footer">
      <div>
        <el-button
          type="plain"
          @click="handleCancel"
          size="small"
          v-if="cancel"
        >
          {{ cancelText }}
        </el-button>
        <el-button type="primary" @click="handleConfirm" size="small">
          {{ confirmText }}
        </el-button>
      </div>
    </slot>
  </el-dialog>
</template>

<script>
export default {
  name: "SysDialog",
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    showFullscreen: {
      type: Boolean,
      default: true,
    },
    title: {
      type: String,
      default: "",
    },
    width: {
      type: String,
      default: "400px",
    },
    cancel: {
      type: Boolean,
      default: true,
    },
    cancelText: {
      type: String,
      default: "取消",
    },
    confirmText: {
      type: String,
      default: "确定",
    },
    atob: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      fullscreen: false,
    };
  },
  computed: {
    show: {
      get() {
        return this.visible;
      },
      set(val) {
        this.$emit("update:visible", val);
      },
    },
    uid() {
      return "dialog_" + this._uid;
    },
  },
  mounted() {},

  methods: {
    handleConfirm() {
      this.$emit("confirm");
    },
    handleCancel() {
      this.show = false;
      this.$emit("cancel");
      setTimeout(() => {
        this.fullscreen = false;
        this.backToOrigin();
      }, 300);
    },
    handleClose() {
      this.show = false;
      this.$emit("close");
      setTimeout(() => {
        this.fullscreen = false;
        this.backToOrigin();
      }, 300);
    },
    handleFullscreen() {
      this.backToOrigin();
      this.fullscreen = !this.fullscreen;
    },
    // 回到原位置
    backToOrigin() {
      this.$nextTick(() => {
        const dialog = document.querySelector(`#${this.uid} .el-dialog`);
        dialog.style.left = "0";
        dialog.style.top = "0";
      });
    },
  },
};
</script>

<style lang="scss" scoped></style>
